// pages/pay/Pay.js
const config = require('../../config.js');
const request = require('../../utils/request.js');
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopinfo:{},
    amount:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShopDetailAction();
  },
 
  /**
     * 获取店铺详情
     */
  getShopDetailAction: function () {
    let url = config.getShopDetailUrl;
    let params = {
      alias: app.globalData.shopAlias
    }
    request.requestPost(url, params)
      .then(data => {
        this.setData({
          shopinfo: data
        });
      }).catch(msg => {
        wx.showToast({
          title: msg,
        })
      });
  },
  
  

  /**
   * 确认支付
   */
  payAction: function () {
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if (!reg.test(this.data.amount)){
        wx.showToast({
          title: '请输入正确的金额',
        })
        return;
    }


    let openId = wx.getStorageSync('openId')
    let shopId = app.globalData.shopId;
    let params = {
      openId: openId,
      shopId: shopId,
      amount: this.data.amount * 100,
    }
    console.log(params);
    request.requestPost(config.payUrl, params)
      .then(data => {
      wx.requestPayment(
        {
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': function (res) {
              //跳支付成功
              wx.redirectTo({
                url: './paysuccess',
              });
          },
          'fail': function (res) {
            console.log(res);
          },
          'complete': function (res) {
            console.log(res);
          }
        })
    }).catch(msg => {
      console.log(msg);
    });

  },

  /**
   * 添加说明
   */
  addInfoDialog: function () {
    
  },

/**
 * 金额输入
 */
  priceInput:function(event){
    this.setData({
      amount: event.detail.value
    })
  }


})